//全局设置axios
axios.defaults.baseURL='http://localhost:80/';
Vue.prototype.$http = axios;

//=========*********=========//
axios.interceptors.request.use(config=>{
    let token = localStorage.getItem("token")
    if(token){
        config.headers["token"] = token;
    }
    return config;
},error => {
    Promise.reject(error)
})
//=========*********=========//
//=========*********=========//
axios.interceptors.response.use(result=>{
    console.log("========")
    let data = result.data;
    if(!data.success && "noLogin"===data.message)
    {
        localStorage.removeItem("token");
        localStorage.removeItem("user");
        alert("你还没有登录，请登录后再操作！")
    }
    console.log("========")
    return result;
},error => {
    Promise.reject(error)
})

//前端拦截器  就是尝试用localstorage获取用户，如果没有获取到，提示登录后再操作
let excludeUrls = ['/index.html',"/about.html"];
let url = location.href; //http://localhost:8081/about.html

let isRun = false;
if (url=="http://localhost:8081/") //首页特殊地址单独放行
{
    isRun = true;
}
else{ //不是特殊首地址，走原来的放行逻辑
    for (let i = 0; i < excludeUrls.length; i++) {
        if (url.indexOf(excludeUrls[i])!=-1)
        {
            //放行
            isRun = true;
            break;
        }
    }
}

if (!isRun) //拦截
{
    let user = localStorage.getItem("user");
    if (!user)
        alert("您还没有登录！请登录后再操作")
}


//解析url参数函数
function parseParamObj(url) {
    let paramsStr = url.substring(url.indexOf("?")+1); //typeId=27
    let paramsArray = paramsStr.split("&"); //typeId=27 xxx=xxx
    let paramObj = {};
    $.each(paramsArray,function(index,param){
        let parmName = param.split("=")[0];
        let parmValue = param.split("=")[1];
        paramObj[parmName] = parmValue;
    })
    return paramObj;
}
